<template>
  <div>
    <van-nav-bar title="个人中心">
      <template #right>
        <van-icon name="setting-o" size="18" />
      </template>
    </van-nav-bar>
    <img
      class="user-poster"
      src="https://img.yzcdn.cn/public_files/2017/10/23/8690bb321356070e0b8c4404d087f8fd.png"
    />
    <van-row class="user-links">
      <van-col span="6">
        <van-icon name="pending-payment" />
        待付款
      </van-col>
      <van-col span="6">
        <van-icon name="records" />
        待接单
      </van-col>
      <van-col span="6">
        <van-icon name="tosend" />
        待发货
      </van-col>
      <van-col span="6">
        <van-icon name="logistics" />
        已发货
      </van-col>
    </van-row>

    <van-cell-group class="user-group">
      <van-cell icon="records" title="全部订单" is-link to="/user/orderlies" />
    </van-cell-group>

    <van-cell-group>
      <van-cell
        icon="points"
        title="我的积分"
        :value="userInfo.memberJifen"
        is-link
      />
      <van-cell
        icon="gold-coin-o"
        title="我的余额"
        :value="userInfo.memberMoney"
        @click="showPopup"
        is-link
      />
      <van-cell
        icon="gold-coin-o"
        title="我的会员"
        :value="userInfo.sysMemberLevel.sysMemberLevelName"
        @click="showPopup"
        is-link
      />
      <van-cell icon="coupon-o" title="我的优惠券" is-link />
      <van-cell icon="gift-o" title="我收到的礼物" is-link />
    </van-cell-group>
    <van-popup
      v-model="show"
      closeable
      position="bottom"
      :style="{ height: '40%' }"
    >
      <van-divider
        :style="{ color: '#1989fa', borderColor: '#1989fa', padding: '0 16px' }"
      >
        选择充值金额</van-divider
      >
      <van-tabs @click="onClick">
        <van-tab title="50元">
          当前余额：{{ userInfo.memberMoney }}<br />
          充值后余额：{{ userInfo.memberMoney + 50 }}<br />
          <van-button size="mini" type="info" @click="gopay(50)"
            >支付</van-button
          >
        </van-tab>
        <van-tab title="100元">
          当前余额：{{ userInfo.memberMoney }}<br />
          充值后余额：{{ userInfo.memberMoney + 100 }}<br />
          <van-button size="mini" type="info" @click="gopay(100)"
            >支付</van-button
          >
        </van-tab>
        <van-tab title="500元">
          当前余额：{{ userInfo.memberMoney }}<br />
          充值后余额：{{ userInfo.memberMoney + 500 }}<br />
          <van-button size="mini" type="info" @click="gopay(500)"
            >支付</van-button
          >
        </van-tab>
      </van-tabs>
    </van-popup>
  </div>
</template>

<script>
import { Row, Col, Icon, Cell, CellGroup, Toast } from "vant";

export default {
  components: {
    [Row.name]: Row,
    [Col.name]: Col,
    [Icon.name]: Icon,
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
  },
  data() {
    return {
      show: false,
      userInfo: {},
    };
  },

  created: function () {
    this.$axios
      .get("/user/member")
      .then((res) => {
        if (res.data.code == 200) {
          this.userInfo = res.data.info;
        }
      })
      .catch((error) => {
        console.log("请求个人信息失败了");
      });
  },

  methods: {
    showPopup() {
      this.show = true;
    },

    onClick(name, title) {
      Toast(title);
      Toast(name);
    },

    gopay(money) {
      let params = new URLSearchParams();
      var no=Math.floor(Math.random()*1000)+100000;
      params.append("outTradeNo", no);
      params.append("subject", "充值");
      params.append("totalAmount", money);
      params.append("description", "【驿站洗衣】余额充值");
      console.log(params);
      this.$axios
        .post("/order/alipay", params)
        .then((res) => {
          console.log(res.data);
          const divForm = document.getElementsByTagName("div");
          if (divForm.length) {
            document.body.removeChild(divForm[0]);
          }
          const div = document.createElement("div");
          console.log(div);
          div.innerHTML = res.data; // data就是接口返回的form 表单字符串
          document.body.appendChild(div);
          document.forms[0].setAttribute("target", "_blank"); // 新开窗口跳转
          document.forms[0].submit();
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>

<style lang="less">
.user {
  &-poster {
    width: 100%;
    height: 53vw;
    display: block;
  }

  &-group {
    margin-bottom: 15px;
  }

  &-links {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    background-color: #fff;

    .van-icon {
      display: block;
      font-size: 24px;
    }
  }
}
</style>
